<?php
include "../../../lib/list.php";

if($_POST['mode']=='insert'){
$barcode 	=	 $_POST['barcode'];
$s_no 		=	 $_POST['s_no'];
$count 		=	 $_POST['count'];
$gap 		=	 $_POST['gap'];
$p_no 		=	 $_POST['p_no'];
$date		=    date("Y-m-d H:i:s",time());
$admin_name		=	$_SESSION['IS_NAME'];
//for문 시작------------------------------------------------------------------>
for($i=0;$i<count($p_no);$i++){
		if(!$p_no[$i]){
		echo("
		    <script>
				      window.alert('$p_no[$i]');
				      window.alert('잘못된 상품입니다.');
				      </script>");
		echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
		return;
		}
		if(!$s_no){
		echo("
		    <script>
				      window.alert('잘못된 점포입니다.');
				      </script>");
		echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
		return;
		}
		if(!$barcode[$i]){
		echo("
		    <script>
				      window.alert('잘못된 상품입니다.');
				      </script>");
				echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
				return;
		}
		if(!$admin_name){
		echo("
			<script>
				      window.alert('잘못된 담당자입니다.');
				      </script>");
				echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			return;
		}
		if(!$count[$i]){
		echo("
			<script>
			      window.alert('상품 갯수를 적어주세요.');
			      </script>");
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			      return;
		}
				else{
					$sql = "SELECT uatp_prdreview.no, uatp_prdgroup.date, uatp_product.price1
										, uatp_product.no AS 'p_no', uatp_prdgroup.note, uatp_prdreview.s_no
										, uatp_product.productname, uatp_product.barcode, uatp_prdreview.price
										, defect.de_count, move.mo_count, ibgo.ib_count, defect.de_price, move.mo_price, ibgo.ib_price
										FROM uatp_prdreview
										INNER JOIN uatp_product ON uatp_prdreview.p_no = uatp_product.no
										INNER JOIN uatp_prdgroup ON uatp_prdreview.date = uatp_prdgroup.date
										
										LEFT OUTER JOIN 
										(SELECT p_no, sum(count) AS de_count, sum(price) AS de_price, unit, s_no
										FROM uatp_prdreview
										WHERE unit =1
										AND s_no = $s_no
										GROUP BY p_no
										) AS defect ON uatp_prdreview.p_no = defect.p_no
										
										LEFT OUTER JOIN 
										(SELECT p_no, sum(count) AS mo_count, sum(price) AS mo_price, unit, s_no
										FROM uatp_prdreview
										WHERE unit =2
										AND s_no = $s_no
										GROUP BY p_no
										) AS move ON uatp_prdreview.p_no = move.p_no
										
										LEFT OUTER JOIN 
										(SELECT p_no, sum(count) AS ib_count, sum(price) AS ib_price, unit, s_no
										FROM uatp_prdreview
										WHERE unit = 0
										AND s_no = $s_no
										GROUP BY p_no
										) AS ibgo ON uatp_prdreview.p_no = ibgo.p_no
										
										WHERE uatp_prdreview.s_no = $s_no AND uatp_prdreview.p_no = $p_no[$i]";
					
				$list 				= DBarray($sql);
				
// 				$lastdate			= DBarray("SELECT * FROM `uatp_stockreview` WHERE p_no = $p_no[$i] ORDER BY date DESC LIMIT 0, 1");
				
				
				
				
				$stockreview		= "INSERT INTO `uatp_stocktmp`
										(`s_no`, `admin_name`, `p_no`, `instock`, `defect`, `move`, `sale`, `nowstock`, `realstock`, `gap`, `date`)
										VALUES 
										(%d, '%s', %d, %d, %d, %d, %d, %d, %d, %d, '%s')
										ON DUPLICATE KEY UPDATE 
										`admin_name`	= '%s'
										, `instock` 	= %d
										, `defect`  	= %d
										, `move`		= %d
										, `sale`		= %d
										, `nowstock`	= %d
										, `realstock`	= %d
										, `gap`			= %d
										, `date`		= '%s'
										;";
				
				$query = sprintf($stockreview
						,$s_no
						,$admin_name
						,$p_no[$i]
						,$list['ib_count']
						,$list['de_count']
						,$list['mo_count']
						,NULL
						,$count[$i]-$gap[$i]
						,$count[$i]
						,$gap[$i]
						,$date
						,$admin_name
						,$list['ib_count']
						,$list['de_count']
						,$list['mo_count']
						,NULL
						,$count[$i]-$gap[$i]
						,$count[$i]
						,$gap[$i]
						,$date);
				
				DBquery($query);
				}
}
//for문 종료------------------------------------------------------------------>
					
				mysql_close();
					
				echo("
									    <script>
							  			      window.alert('정상 입력 됐습니다.');
									         </script>"
				);
				echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
}